<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Framework\Exceptions;

/**
 * Database query exception
 *
 * @package NRG\Framework\Exceptions
 */
class QueryException extends BaseException
{
    /**
     * Query failed
     *
     * @var int
     */
    const QUERY_FAILED = 1;

    /**
     * Empty result
     *
     * @var int
     */
    const EMPTY_RESULT = 2;

    /**
     * Empty query
     * 
     * @var int
     */
    const NULL_QUERY = 3;

    /**
     * Expected multiple rows
     * 
     * @var int
     */
    const SINGLE_ROW = 4;

    /**
     * Expected a single row
     *
     * @var int
     */
    const MULTI_ROW = 5;
    
    /**
     * Exception type
     *
     * @var const
     */
    protected $_type;

    /**
     * Key that caused exception
     *
     * @var mixed
     */
    protected $_key;
    
    /**
     * Query
     *
     * @var string
     */
    protected $_query;

    /**
     * Query params
     *
     * @var array
     */
    protected $_params;

    /**
     * Constructor
     *
     * @param int $type
     * @param string $query
     * @param array $params
     */
    public function __construct($type, $query, $params=array(), $message="")
    {
	$this->_type = $type;
	$this->_query = $query;
	$this->_params = $params;
        $this->createMessage($message);
    }

    /**
     * Get exception type
     *
     * @return int
     */
    public function getType()
    {
	return $this->_type;
    }

    /**
     * Get query that caused exception
     *
     * @return string
     */
    public function getQuery()
    {
	return $this->_query;
    }

    /**
     * Get query parameters that go along with query
     *
     * @return array
     */
    public function getParams()
    {
	return $this->_params;
    }

    /**
     * Get exception message
     *
     * @return string
     */
    public function createMessage($message="")
    {
	switch($this->_type)
	{
	    case self::QUERY_FAILED:
		$this->message = "Database query failed: " . $this->_query;
		break;
	    case self::EMPTY_RESULT:
		$this->message = "Query result is empty: " . $this->_query;
		break;
	    case self::NULL_QUERY:
		$this->message = "Query cannot be null";
		break;
	    case self::SINGLE_ROW:
		$this->message = "Expected multiple rows: " . $this->_query;
		break;
	    case self::MULTI_ROW:
		$this->message = "Expected a single row: " . $this->_query;
		break;
	    default:
		$this->message = "Unknown query exception";
		break;
	}

    if (!empty($message))
        $this->message.=". ".$message;

	return $this->message;
    }

    /**
     * To string
     *
     * @return string
     */
    public function __toString()
    {
	return $this->getMessage();
    }
}
